package guoanmaker.operator.business.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.model.PerviderDictionary;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.service.ProviderDictionaryServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

/**
 * 
* @ClassName: ProviderDictionaryController
* @Description:数据字典 (这里用一句话描述这个类的作用) 
* @author lihongyu
* @date 2017年7月10日 下午3:05:42 
*  
 */
@RestController
@RequestMapping(ApiController.DICTIONARY_URL)
public class ProviderDictionaryController {
	@Autowired
	private ProviderDictionaryServiceInterface providerDictionaryServiceInterface;
	 @ApiOperation(value = "身份类型列表", notes = "身份类型列表 ")
	    @PostMapping(value = "selectByfatheridList")
	public AjaxJson selectByfatheridList(String fatherID){
		 AjaxJson ajaxJson=new AjaxJson();
		List<PerviderDictionary> pds=providerDictionaryServiceInterface.selectdicList(fatherID);
		if(pds.size()!=0){
			ajaxJson.setSuccess(true).setStatus(200).setData(pds);
		}else{
			ajaxJson.setSuccess(true).setStatus(250);
		}
		return ajaxJson;
	}
	 /**
	  * 
	     * @Title: savePerviderDictionary  
	     * @Description:   先根据dictName,fatherID查询单位是否存在，否则保存。
	     * @param @param dictName
	     * @param @param fatherID
	     * @return AjaxJson      
	     * @throws
	  */
	 @ApiOperation(value = "新增商品单位", notes = "保存新增商品单位")
	 @PostMapping(value = "savePerviderDictionary")
	 public AjaxJson savePerviderDictionary(String dictName, String fatherID) {
		 AjaxJson ajaxJson=new AjaxJson();
		 ReturnResult returnResult = new ReturnResult();
		 try {
			 returnResult = providerDictionaryServiceInterface.savePerviderDictionary(dictName, fatherID);
			 ajaxJson.setSuccess(true).setStatus(200).setData(returnResult).setMsg("操作成功");
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(250).setData(returnResult).setMsg("操作失败");
		}
		 return ajaxJson;
	 }
	 /**
	  * 
	     * @Title: selectBydictNamePage  
	     * @Description: 通过商品单位分页查询  
	     * @param @param dictName
	     * @param @param page
	     * @param @param size
	     * @return AjaxJson      
	     * @throws
	  */
	 @ApiOperation(value = "通过商品单位分页查询", notes = "通过商品单位分页查询")
	 @PostMapping(value = "selectBydictNamePage")
	 public AjaxJson selectBydictNamePage(String dictName, @RequestParam(value = "page", defaultValue = "0") Integer page,
			 @RequestParam(value = "size", defaultValue = "10")Integer size) {
		 AjaxJson ajaxJson=new AjaxJson();
		 try {
			ajaxJson.setSuccess(true).setStatus(200).setData(providerDictionaryServiceInterface.selectBydictNamePage(dictName, page, size)).setMsg("操作成功");
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(250).setData(null).setMsg("操作失败");
		}
		 return ajaxJson;
	 }
	 /**
	  * 
	     * @Title: deletePerviderDictionaryById  
	     * @Description: 通过id删除商品单位
	     * @param @param id
	     * @return AjaxJson      
	     * @throws
	  */
	 @ApiOperation(value ="通过id删除商品单位", notes ="通过id删除商品单位")
	 @PostMapping(value = "deletePerviderDictionaryById")
	 public AjaxJson deletePerviderDictionaryById(String id) {
		 AjaxJson ajaxJson=new AjaxJson();
		 ReturnResult returnResult = new ReturnResult();
		 
		 try {
			 returnResult = providerDictionaryServiceInterface.deletePerviderDictionaryById(id);
			 ajaxJson.setSuccess(true).setStatus(200).setData(returnResult).setMsg("操作成功");
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(250).setData(returnResult).setMsg("操作失败");
		}
		 return ajaxJson;
	 }
}
